/**
 * @program: LeetCode
 * @description: LeetCode : 541.反转第k个字符串
 * @author: WXY
 * @create: 2023-01-15 15:57
 * @Version 1.0
 **/
public class Num541_reverseStr {
    public static String reverseStr(String str, int k) {
        char[] string = str.toCharArray();
        for (int i = 0; i < string.length; i += k * 2) {
            int start = i;
            int end = Math.min(string.length - 1, start + k - 1);
            while (start < end) {
                swap(string, start, end);
                start++;
                end--;
            }
        }
        return new String(string);
    }

    private static void swap(char[] string, int l, int r) {
        char ch = string[l];
        string[l] = string[r];
        string[r] = ch;
    }
}
